#!/usr/bin/perl

use strict;
use DBI;
use CGI;

# log_reader.pl
#  - Jamie Stevens 2010-08-05

# This script is responsible for generating the webpage for any
# particular log.

# start the webpage
my $in=CGI->new;

my %input=$in->Vars;

# open up a connection to the online_log database
my $dsn="dbi:Pg:dbname=online_log;host=localhost;port=5432";
my $db_user="logread";
my $db_password="logread";
my $dbh=DBI->connect($dsn,$db_user,$db_password,
		     {RaiseError => 1, AutoCommit => 1});

# get the info about the log we've been asked to view
my $this_log_query="SELECT project_code,creation_time,observer,".
    "options,instrument,identifier FROM log WHERE log_id=".
    $input{"log_id"};
my $this_log=$dbh->prepare($this_log_query);
$this_log->execute;
my @log_entries;
while(@log_entries=$this_log->fetchrow){
    last;
}
$this_log->finish;

$dbh->disconnect;

print $in->header;

print $in->start_html(-title => 'Online Log',
		      -style => [{'src'=>"/dojo/resources/dojo.css"},
				 {'src'=>"log_style.css"}],
		      -script => [{-type =>'text/javascript',
				  -src => "/dojo/dojo.js"},
				  {-type =>'text/javascript',
				  -src => "log_reader.js"}]);

print "<table class='topbar'><tr><td><a href='http://www.csiro.au/'><img src='csiro50spaced.gif'></a></td>\n";
print "</table>\n";

print "<form name='page_values' id='page_form'>\n";
print "<input type='hidden' name='log_id' id='log_id' value='".
    $input{"log_id"}."' />\n";
print "</form>\n";

print "<div class='leftnav' id='left'>\n";
print "<table><tr><td>\n";
print "<button id='AddEntry'>Add Log Entry</button>\n";
print "</td><td>\n";
print "<button id='AddAttachment'>Add Attachment</button>\n";
print "</td></tr></table>\n";
print "<table><tr><th>Project:</th><td>".$log_entries[0]."</td></tr>\n";
print "<tr><th>Observer:</th><td>".$log_entries[2]."</td></tr>\n";
print "<tr><th>Instrument:</th><td>".$log_entries[4]."</td></tr>\n";
print "<tr><th>Identifier:</th><td><span id='editIdentifier'>";
if ($log_entries[5] eq ""){
    print "none";
} else {
    print $log_entries[5];
}
print "</span></td></tr>\n";
print "</table>\n";
print "</div>\n";
print "<div class='rightcontent' id='right'>\n";
print "<table class='logEntriesTitle'>\n";
print "<tr><th class='logEntriesTitleth'>UTC</th><td class='logEntriesTitletd'>".
    "Description</td></tr>\n";
print "</table><br />\n";
print "<table id='logEntries' class='logEntries'>";
# the first entry in the right column is always the time the log was created
my $time_th=$log_entries[1];
$time_th=~s/^(.*)\s(.*)\..*$/$1\<br \/\>$2/;
print "<tr><th>".$time_th."</th><td>Log creation time</td></tr>\n";
print "</table>\n";
print "</div>\n";

print $in->end_html;



